/*
 * PrinterDelegate.cpp  0.4.0 / Dec 04, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "PrinterDelegate.h"

#include "SwingDelegate.h"

// PrinterDelegate :: Protected Constructor. -----------------------------------

/*!
 * \brief Constructs new PrinterDelegate object.
 *
 * \param env the JNIEnv pointer
 * \param obj the Printer java object
 */
PrinterDelegate::PrinterDelegate(JNIEnv *env, jobject obj)
  : QObject()
{
  m_wobjref = JNIWrapper::newWeakGlobalRef(env, obj);
  if (m_wobjref) {
    m_printer = new QPrinter();
  }
}

// PrinterDelegate :: Protected Destructor. ------------------------------------

/*!
 * \brief Destroys the PrinterDelegate object.
 */
PrinterDelegate::~PrinterDelegate()
{
  if (m_wobjref) {
    JNISwingWrapper::deleteWeakGlobalRef(m_wobjref);
  }
  if (m_printer) {
    delete m_printer;
  }
}

// PrinterDelegate :: Native Calls. --------------------------------------------

/*!
 * \brief Destroys the PrinterDelegate object.
 *
 * \param env the JNIEnv pointer
 */
void PrinterDelegate::destroy(JNIEnv *env)
{
  Q_UNUSED(env);
  if (SwingDelegate::isDisposed()) {
    delete this; // OK - referenced only from java
  } else {
    deleteLater();
  }
}

/*!
 * \brief Displays page dialog.
 *
 * \param env the JNIEnv pointer
 */
void PrinterDelegate::pageDialog(JNIEnv *env)
{
  Q_ASSERT(env);
  SWING_REENTRANT(pageDialog, SWING_ENV_ARG)
  QPageSetupDialog pageSetup(m_printer, QApplication::activeWindow());
  pageSetup.exec();
}

/*!
 * \brief Displays printer dialog.
 *
 * \param env the JNIEnv pointer
 * 
 * \return \c it the dialog is accepted
 */
jboolean PrinterDelegate::printerDialog(JNIEnv *env)
{
  Q_ASSERT(env);
  SWING_REENTRANT_RETURN(printerDialog, jboolean, SWING_ENV_ARG)
  QPrintDialog printDialog(m_printer, QApplication::activeWindow());
  return (printDialog.exec() == QDialog::Accepted);
}

// PrinterDelegate :: Static Native Calls. -------------------------------------

/*!
 * \brief Initializes PrinterDelegate class.
 *
 * \param env the JNIEnv pointer
 * \param cls the Printer class
 *
 * \exception FatalError if failed
 */
void PrinterDelegate::initialize(JNIEnv *env, jclass cls)
{
  Q_ASSERT(env);
  Q_ASSERT(cls);
  Q_ASSERT(!sm_delegateFID);
  if (!INIT_UFID(delegate, cls, "J") || !INIT_UMID(create, cls, "()J")) {
    FATAL_LOGGED_ERROR("PrinterDelegate initialization failed");
  }
}

/*!
 * \brief Returns new instance of the delegate object.
 *
 * \param env the JNIEnv pointer
 * \param obj the Printer object
 * 
 * \return new instance of the delegate object
 */
jlong PrinterDelegate::newInstance(JNIEnv *env, jobject obj)
{
  if (JNISwingWrapper::isSwingThread(env)) {
    return reinterpret_cast<jlong>(new PrinterDelegate(env, obj));
  }
  return SwingUtilitiesDelegate::invokeLongAndWait(env, obj, sm_createMID);
}

// PrinterDelegate :: Public Static Member Function. ---------------------------

/*!
 * \brief Returns QPrinter object of the specified Swing Printer object.
 *
 * \param env the JNIEnv pointer
 * \param printer the Swing printer object
 * 
 * \return QPrinter object of the specified Swing Printer object
 */
QPrinter *PrinterDelegate::toQPrinter(JNIEnv *env, jobject printer)
{
  return PRINTER_CAST(JNIWrapper::getLongField(env, printer,
                                               sm_delegateFID))->qPrinter();
}

// PrinterDelegate :: Private Static Data Members. -----------------------------

jfieldID PrinterDelegate::sm_delegateFID = 0;
jmethodID PrinterDelegate::sm_createMID = 0;
